package com.zgjkhis.modules.system.rest;

import com.zgjkhis.domain.PageResult;
import com.zgjkhis.exception.ServiceException;
import com.zgjkhis.logging.aop.log.Log;
import com.zgjkhis.modules.system.domain.ConversionFactor;
import com.zgjkhis.modules.system.service.ConversionFactorService;
import com.zgjkhis.modules.system.service.dto.ConversionFactorDto;
import com.zgjkhis.modules.system.service.dto.ConversionFactorQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @date 2019-04-10
 */
@Api(tags = "系统：系数管理")
@RestController
@RequestMapping("/api/conversionFactor")
public class ConversionFactorController {

    private static final String ENTITY_NAME = "conversionFactor";
    private final ConversionFactorService conversionFactorService;

    public ConversionFactorController(ConversionFactorService conversionFactorService) {
        this.conversionFactorService = conversionFactorService;
    }

    @Log("分页查询系数")
    @ApiOperation("分页查询系数")
    @GetMapping(value = "/all")
//    @PreAuthorize("@el.check('admin','conversionFactor:list')")
    public ResponseEntity<List<ConversionFactor>> all() {
        return new ResponseEntity<>(conversionFactorService.queryAll(new ConversionFactorQueryCriteria()), HttpStatus.OK);
    }

    @Log("查询系数")
    @ApiOperation("查询系数")
    @GetMapping
//    @PreAuthorize("@el.check('admin','conversionFactor:list')")
    public ResponseEntity<PageResult<ConversionFactorDto>> getDicts(ConversionFactorQueryCriteria resources, Pageable pageable) {
        return new ResponseEntity<>(conversionFactorService.queryAll(resources, pageable), HttpStatus.OK);
    }

    @Log("新增系数")
    @ApiOperation("新增系数")
    @PostMapping
//    @PreAuthorize("@el.check('admin','conversionFactor:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ConversionFactor resources) {
        if (resources.getId() != null) {
            throw new ServiceException("A new " + ENTITY_NAME + " cannot already have an ID");
        }
        return new ResponseEntity<>(conversionFactorService.save(resources), HttpStatus.OK);
    }

    @Log("修改系数")
    @ApiOperation("修改系数")
    @PutMapping
//    @PreAuthorize("@el.check('admin','conversionFactor:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ConversionFactor resources) {
        resources.setUpdateTime(new Date());
        conversionFactorService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("删除系数")
    @ApiOperation("删除系数")
    @DeleteMapping(value = "/{id}")
//    @PreAuthorize("@el.check('admin','conversionFactor:del')")
    public ResponseEntity<Object> delete(@PathVariable Long id) {
        conversionFactorService.removeById(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
